<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">


<!--     寝室长提交的当天信息管理 模块：   -->



<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>打卡记录管理</title>
    <link rel="stylesheet" href="/static/layui/css/layui.css" th:href="@{/static/layui/css/layui.css}">
</head>

<body>
<input type="text" id="ctx" hidden="hidden" th:value="${#request.getContextPath()}">
<!-- 内容主体区域 -->
<div>
    <div class="search-div">
        <div class="">
            <div class="layui-inline">
                <label class="layui-form-label">寝长学号：</label>
                <div class="layui-input-inline">
                    <input class="layui-input" id="studentNumber" name="studentNumber" autocomplete="off">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">姓名：</label>
                <div class="layui-input-inline">
                    <input class="layui-input" id="name" name="name" autocomplete="off">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">提交日期：</label>
                <div class="layui-input-inline">
                    <input type="date" name="submitTime" id="submitTime" autocomplete="off" class="layui-input">
                </div>
            </div>
            <button class="layui-btn" data-type="reload" id="search">搜索</button>
        </div>
    </div>
    <table id="tb-book" lay-filter="tb-book"></table>

</div>

<!--编辑表单-->
<script type="text/html" id="book-toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="remove"><i class="layui-icon">&#xe640;</i>删除选中记录
        </button>
        <button class="layui-btn layui-btn-sm" lay-event="refresh"><i class="layui-icon">&#xe666;</i>刷新
        </button>
    </div>
</script>

<script type="text/html" id="barOps">
    <a class="layui-btn layui-btn-sm" lay-event="edit"><i class="layui-icon">&#xe642;</i> </a>
</script>

<script src="/static/js/jquery-1.11.3.min.js" th:src="@{/static/js/jquery-1.11.3.min.js}"></script>
<script src="/static/layui/layui.all.js" th:src="@{static/layui/layui.all.js}"></script>
<script src="/static/js/util.js" th:src="@{/static/js/util.js}"></script>

<!--ctx-->
<script th:replace="~{fragment::ctx}"/>

<script>
    var element, layer, laydate, table, form,userTable;


    function reload(){
        userTable.reload();
    }

    $(function () {
        // 使用模块
        layui.use(['element', 'layer', 'laydate', 'table', 'form'], function () {
            element = layui.element;
            layer = layui.layer;
            laydate = layui.laydate;
            table = layui.table;
            form = layui.form;
        });

        //第一个实例
        userTable = table.render({
            elem: '#tb-book'
            , height: 515
            , url: ctx+'api/dailyReport/list' //数据接口
            , page: true //开启分页
            , toolbar: '#book-toolbar'
            ,id: 'tb-book'
            , cols: [
                [ //表头
                    //寝室长学号、姓名、寝室号、联系方式、寝室内所有身体情况、所需药品、补充
                    {type: 'checkbox', fixed: 'left'}
                    , {field: 'id', title: 'ID', width: 30, sort: true, fixed: 'left',hide:true}
                    , {field: 'studentNumber', title: '寝室长学号', width: 90}
                    , {field: 'name', title: '姓名', width: 90}
                    , {field: 'site', title: '寝室号', width: 200}
                    , {field: 'phone', title: '联系方式', width: 150}
                    , {field: 'submitTime', title: '提交时间', width: 210}
                    , {field: 'information', title: '寝室异常身体情况', width: 120}
                    , {field: 'need', title: '相应所需药品', width: 120}
                    , {fixed: 'right', title: '操作', toolbar: '#barOps', width: 120}
                ]
            ]
        });

        //监听表单提交
        // 修改
        form.on('submit(saveBook)', function (data) {
            layer.alert(JSON.stringify(data.field));
            $.ajax({
                url: ctx+'api/dailyReport/update',
                type: 'POST',
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(data.field),
                success: function (result) {
                    if (result.code === 200) {
                        // layer.msg("修改成功！", {icon: 6});
                        cleanForm("#saveBook");
                        if(data.field.id.length>0){
                            layer.closeAll();//关闭所有的弹出层
                        }
                        userTable.reload();
                    }else {
                        layer.alert(result.message);
                    }
                }
            });
            return false;
        });

        //工具栏事件
        table.on('toolbar(tb-book)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var checkData = checkStatus.data;
            var ids = [];
            switch (obj.event) {
                // 删除
                case 'remove':
                    if (checkData.length == 0) {
                        layer.alert('请选择要操作的行');
                    } else {
                        layer.confirm('确定要删除吗？', function (index) {
                            for (var i = 0; i < checkData.length; i++) {
                                ids.push(checkData[i].id);
                            }
                            //layer.alert(JSON.stringify(ids));
                            $.ajax({
                                url: ctx+'api/dailyReport/remove',
                                type: 'POST',
                                contentType: "application/json",
                                dataType: "json",
                                data: JSON.stringify(ids),
                                success: function (result) {
                                    if (result.code == 200) {
                                        setTimeout(function () {
                                            layer.closeAll();//关闭所有的弹出层
                                            userTable.reload();
                                        }, 300);
                                    }else {
                                        layer.msg("操作失败！", {icon: 5});
                                    }
                                }
                            });
                        });
                    }
                    break;
                case 'refresh':
                    userTable.reload();
                    break;
                case 'getCheckData':
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选')
                    break;
            }
        });

        // 监听工具条
        table.on('tool(tb-book)', function (obj) {
            var data = obj.data;
            // 修改
            if (obj.event === 'edit') {
                top.layer.open({
                    type: 2,
                    offset: '10px',
                    title: "记录修改",
                    area: ['800px', '660px'],
                    content: [ctx+'dailyReportEdit/'+data.id]
//    TODO
                });
            }
        });

        // 搜索
        $('#search').click(function () {
            var name = $('#name').val();
            var studentNumber = $('#studentNumber').val();
            var submitTime = $('#submitTime').val();
            table.reload('tb-book', {
                url: ctx+'api/dailyReport/list'
                ,where: {
                    name:name,
                    studentNumber:studentNumber,
                    submitTime:submitTime
                }
                ,page: {
                    curr: 1
                }
            });
        });
    });


</script>
</body>

</html>